<template>
  <div class="updata-name">
    <van-nav-bar
      title="设置昵称"
      left-text="返回"
      right-text="按钮"
      left-arrow
      @click-left="$emit('close')"
      @click-right="onComfirm"
    />

    <!-- 编辑区 -->
    <div class="field-wrip">
      <van-field
        v-model="message"
        rows="2"
        autosize
        type="textarea"
        maxlength="20"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateName',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      message: this.value
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onComfirm () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true
      })
      const message = this.message
      if (message.length === 0) return this.$toast('不能为空')
      try {
        await updateUserProfile({
          name: this.message
        })
        // 传值到父
        this.$emit('input', message)
        // 关闭弹出层
        this.$emit('close')
        this.$toast('更新成功')
      } catch (error) {
        this.$toast('更新失败')
      }
    }
  }
}
</script>
<style lang="less" scoped>
.field-wrip {
  padding: 20px;
}
</style>
